import logging
from functools import partial

from PyQt5.QtWidgets import QPushButton, QTableWidget

from frw.vacwx import IVacWidget
from frw.qt5_ext import extract_wx_from_container

class SubPageTabBar(IVacWidget):
    """ 含有多个子页面（self.show_stack_wx）的TabBar，
        功能包含对*顶层tab层*按钮的管理，以及table_wx的切换子页面
    """
    def __init__(self, parent, list_tab_names):
        self.parent = parent
        super().__init__(parent)
        self.list_tab_names = list_tab_names
        self._tab_btns_init()

        self.list_tab_btn_clicked_extend_callback = []  # tab-btn的补充动作

    def _tab_btns_init(self):
        for index, btn_name in enumerate(self.list_tab_names):
            btn = QPushButton(btn_name)
            self.tab_btn_layout.addWidget(btn)
            # connect to slot
            btn.clicked.connect(partial(self.on_tab_btn_clicked, index))

    def on_tab_btn_clicked_extend(self, callback, *args):
        self.list_tab_btn_clicked_extend_callback.append((callback, args))

    def on_tab_btn_clicked(self, index):
        """ 切换stack的子页面，并更新tab-btn的显示样式 """
        stack_wx = self.parent.stack_wx  # 这里有点不符合范式，但可以减少__init__的一个传参
                                         # 实际上，对stack_wx的强耦合不可避免
        stack_wx.setCurrentIndex(index)

        # 切换title-btn的显示样式
        for layout_index in range(self.tab_btn_layout.count()):
            tab_btn = self.tab_btn_layout.itemAt(layout_index).widget()
            if index == layout_index:
                tab_btn.setStyleSheet("background-color: rgb(170, 0, 255);")
            else:
                tab_btn.setStyleSheet("background-color: rgb(220, 226, 228);")

        # 补充操作
        if not self.list_tab_btn_clicked_extend_callback:  return
        for tuple_operation in self.list_tab_btn_clicked_extend_callback:
            callback, args = tuple_operation
            callback(*args)


class SubPageWithTableVac(IVacWidget):
    """ 某个子页面中，存在QTableWidget控件 """
    def __init__(self, parent, list_tab_names=None):
        super().__init__(parent)

        self.table_wx = None
        self.stack_wx = self.show_stack_wx  # 强耦合
        if list_tab_names:
            self.tab_bar = SubPageTabBar(self, list_tab_names)
            self.title_layout.addWidget(self.tab_bar)  # 又一个强耦合，但可以省代码
            # 向SubPageTabBar传递执行动作
            self.tab_bar.on_tab_btn_clicked_extend(self.update_table_wx)

    def update_table_wx(self):
        self.table_wx = self.curr_table_wx()
        logging.debug("更新当前self.table_wx指针 -> [{}]".format(self.table_wx))

    # @final
    def curr_table_wx(self, stack_index=None):
        """ return the current stackWidget-page's QTableWidget """
        if stack_index:
            curr_stack_frame = self.stack_wx.widget(stack_index)
        else:
            curr_stack_frame = self.stack_wx.currentWidget()

        table_wx = extract_wx_from_container(curr_stack_frame, type=QTableWidget)
        return table_wx

    # @final
    def table_data_clear(self, table_wx):
        table_wx.clearContents()
        table_wx.setRowCount(0)

    # @abstract
    def table_showing_style(self, table_wx):
        # header = table_wx.horizontalHeader()
        # for column_index in range(len(self.list_tab_names)):
        #     header.setSectionResizeMode(column_index, QHeaderView.Stretch)  # ResizeToContents
        pass

    # @abstract
    def table_data_update(self, table_wx, *var_args):
        # self.table_data_clear(table_wx)
        pass

    # @abstract
    def table_init(self):
        # 初始化table_wx中的数据
        # table_wx = self.curr_table_wx()
        # if table_wx:
        #     self.table_data_update(table_wx)
        #     self.table_showing_style(table_wx)
        pass
